package com.langsin.chenxinhao;


import com.langsin.bean.Goods;
import com.langsin.bean.OrderDetail;
import com.langsin.bean.Orders;
import com.langsin.bean.User;
import com.langsin.chenxinhao.mapper.GoodsMapper;
import com.langsin.chenxinhao.mapper.OrdersMapper;
import com.langsin.chenxinhao.mapper.UserMapper;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


public class MyTest {
    SqlSession sqlSession = null;

    @Test
    public void testUseGoOdsLazy() throws Exception {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.queryUserGoods(702);
        System.out.println(user.getCname() + "," + user.getAddress());

        Thread.sleep(5000);

        for (Orders orders : user.getOrdersList()) {
            System.out.println(orders.getOrderId());

            Thread.sleep(5000);

            for (OrderDetail orderDetail : orders.getOrderDetailList()) {
                System.out.println(orderDetail.getDetailId());
                Thread.sleep(5000);
                System.out.println(orderDetail.getGoods());
            }
        }
    }

    @Test
    public void testUserGoods() throws Exception {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.queryUserGoods(702);
        System.out.println(user);
        System.out.println("用户订单：");
        for (Orders orders : user.getOrdersList()) {
            System.out.println(orders + "\t");
            System.out.print("订单详情：");
            for (OrderDetail orderDetail : orders.getOrderDetailList()) {
                System.out.print(orderDetail + "\t");
            }
        }
    }

    @Test
    public void testOrderUser() throws Exception {
        OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class);
        Orders orders = ordersMapper.queryOrdersAndUserByOrderId(1002);
//        System.out.println(orders);
//        System.out.println(orders.getUser());
        System.out.println(orders.getOrderId() + "~" + orders.getOrderDate());
        /*模拟程序度过一段时间*/
        Thread.sleep(10000);
        System.out.println(orders.getUser());
    }

    @Test
    public void testSetTag() throws Exception {
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        Goods goods = new Goods();
        goods.setGoodsId(9010);
        goods.setGoodsName("iPhone");
        goods.setShopPrice(15.6);
        goods.setGoodsStock(600);
        int count = goodsMapper.updateByCondition(goods);
        System.out.println(count);

    }

    @Test
    public void test1M() throws Exception {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.queryUserOrders(702);
        System.out.println(user);
        for (Orders orders : user.getOrdersList()) {
            System.out.println(orders);
        }
    }

    @Test
    public void test2() throws Exception {
        Goods goods = new Goods();
//        goods.setGoodsName("%C%");
        goods.setShopPrice(18.8);
//        goods.setGoodsStock(900);
        GoodsMapper goodsmapper = sqlSession.getMapper(GoodsMapper.class);
        List<Goods> goodsList = goodsmapper.queryByCondition(goods);
        goodsList.forEach(System.out::println);
        System.out.println(goodsList);
    }

    @Test
    public void test1() throws Exception {
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Integer> ids = new ArrayList<>();
        ids.add(9003);
        ids.add(9008);
        ids.add(9009);
        List<Goods> goods = goodsMapper.queryByIds(ids);
        for (Goods good : goods) {
            System.out.println(good);
        }

        Goods goods1 = goodsMapper.queryByIdAno(9001);
        System.out.println(goods1);

    }

    // 每次单元测试之前执行
    @Before
    public void before() throws Exception {
        // 读取配置文件
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        // 工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(stream);
        // 获取一个会话连接，默认不自动提交
        sqlSession = sqlSessionFactory.openSession();
    }

    // 每次单元测试之后执行
    @After
    public void after() {
        sqlSession.commit();
        sqlSession.close();
    }

}
